package cn.ac.iie.ban.data.exchange.runner.tokenBucket.startup;

import cn.ac.iie.ban.data.exchange.runner.tokenBucket.server.HttpService;
import cn.ac.iie.ban.data.exchange.runner.tokenBucket.util.PairList;
import cn.ac.iie.ban.data.exchange.runner.tokenBucket.util.RuntimeEnv;
import cn.ac.iie.ban.data.exchange.runner.tokenBucket.util.ServiceInfo;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author admin
 */
public class TokenBucketStartUp {

    static Logger logger = null;

    static {
        PropertyConfigurator.configure(ClassLoader.getSystemClassLoader().getResource("log4j.properties"));
        logger = Logger.getLogger(TokenBucketStartUp.class.getName());
    }
    static HttpService service;

    public static void main(String[] args) {
//            System.out.println("serverStart.......");
        try {
            RuntimeEnv.init();
            Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {

                    if (service != null) {
                        try {
                            logger.info("try to stop the service gracefully.");
                            service.stopGracefully();
                            logger.info("finished stopping the service gracefully.");
                        } catch (Exception ex) {
                            logger.error("cannot stop the service gracefully", ex);
                            try {
                                logger.info("try to shut down the service");
                                service.shutdown();
                                logger.info("shut down the service successfully.");
                            } catch (Exception ex1) {
                                logger.error("cannot shutdown the service", ex1);
                                return;
                            }
                        }
                    }
                }
            });
            service = new HttpService(
                    RuntimeEnv.getParamAsString(RuntimeEnv.SERVER_IP),
                    RuntimeEnv.getParamAsInteger(RuntimeEnv.SERVER_PORT),
                    RuntimeEnv.getParamAsInteger(RuntimeEnv.SERVER_PARALLEL_SIZE));
            PairList<Integer, Integer> speedList;
            if (RuntimeEnv.getParamAsInteger(RuntimeEnv.LIMIT_SPEED) != null) {
                speedList = new PairList<>(
                        RuntimeEnv.getParamAsInteger(RuntimeEnv.LIMIT_SPEED),
                        RuntimeEnv.getParamAsInteger(RuntimeEnv.LIMIT_SPEED_INTERVAL_SEC));
            } else {
                speedList = new PairList<>();
            }
            PairList<Integer, Integer> sizeList;
            if (RuntimeEnv.getParamAsInteger(RuntimeEnv.LIMIT_SIZE) != null) {
                sizeList = new PairList<>(
                        RuntimeEnv.getParamAsInteger(RuntimeEnv.LIMIT_SIZE),
                        RuntimeEnv.getParamAsInteger(RuntimeEnv.LIMIT_SIZE_INTERVAL_SEC));
            } else {
                sizeList = new PairList<>();
            }

            service.initSpeedLimitHelper(
                    new ServiceInfo(
                            RuntimeEnv.getParamAsStrings(RuntimeEnv.META_URL),
                            RuntimeEnv.getParamAsString(RuntimeEnv.META_SUFFIX)),
                    RuntimeEnv.getParamAsInteger(RuntimeEnv.META_UPDATE_INTERVAL),
                    RuntimeEnv.getParamAsInteger(RuntimeEnv.LIMIT_TOTAL_PER_SEC),
                    speedList, sizeList,
                    RuntimeEnv.getAllParamAsInteger(RuntimeEnv.LIMIT_SPEED_SPECIFIC_PRIFIX));

            service.initStatistic(
                    RuntimeEnv.getParamAsString(RuntimeEnv.STATISTIC_RMQ_NAMESRV),
                    RuntimeEnv.getParamAsString(RuntimeEnv.STATISTIC_RMQ_TOPIC),
                    RuntimeEnv.getParamAsInteger(RuntimeEnv.STATISTIC_INTERVAL_SEC));
            service.start();
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex);
        }
    }
}
